﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevComponents.DotNetBar.Controls;
using SharpErp.Data.Common.View;

namespace SharpErp.UI.BasicData.Controls
{
    public partial class ProductionSelectControl : Common.BaseUserControl
    {
        public enum ItemOperationType
        {
            OpSelected, OpCanceled,
        }
        public delegate void ItemSelectionChanged(ItemOperationType opType, int typeIndex, Data.Common.Entity.BaseEntity entity);
        
        public ItemSelectionChanged OnItemSelectionChanged { get; set; }

        public bool SingleSelect { get; set; }

        public HashSet<int>[] SelectedIds = new HashSet<int>[11];

        public ProductionSelectControl()
        {
            InitializeComponent();
        }

        public void InitData()
        {
            this.dgClass1.AutoGenerateColumns = this.dgClass2.AutoGenerateColumns = this.dgClass3.AutoGenerateColumns = this.dgClass4.AutoGenerateColumns =
                this.dgClass5.AutoGenerateColumns = this.dgClass6.AutoGenerateColumns = this.dgClass7.AutoGenerateColumns = this.dgClass8.AutoGenerateColumns =
                this.dgClass9.AutoGenerateColumns = this.dgClass10.AutoGenerateColumns = false;
            this.dgClass1.DataSource = Service.GetSysProductionFirstClass();
        }

        private void dgClass1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            DataGridView dg = (DataGridView)sender;
            // Current row record
            string rowNumber = (e.RowIndex + 1).ToString();

            // Format row based on number of records displayed by using leading zeros
            while (rowNumber.Length < dg.RowCount.ToString().Length) rowNumber = "0" + rowNumber;

            // Position text
            SizeF size = e.Graphics.MeasureString(rowNumber, this.Font);
            if (dg.RowHeadersWidth < (int)(size.Width + 20)) dg.RowHeadersWidth = (int)(size.Width + 20);

            // Use default system text brush
            Brush b = SystemBrushes.ControlText;

            // Draw row number
            e.Graphics.DrawString(rowNumber, dg.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y + ((e.RowBounds.Height - size.Height) / 2));
        }

        public Data.Common.View.QueryGoodsInfoView getSelectedGoodsView()
        {
            var item = dgClass8.CurrentRow.DataBoundItem as Data.Common.Entity.ProductionEightClass;
            var nineClassItem = dgClass9.CurrentRow.DataBoundItem as Data.Common.Entity.ProductionNineClass;
            var tenClassItem = dgClass10.CurrentRow.DataBoundItem as Data.Common.Entity.ProductionTenClass;
            return new QueryGoodsInfoView
            {
                Name = item.Name,
                Code = item.Code,
                Length = item.Length + "*" + item.Width + "*" + (dgClass4.CurrentRow.DataBoundItem as Data.Common.Entity.ItemForthClass)?.Name,
                Facility = item.Facility,
                Standard = item.Standard,
                Unit = item.Unit,
                ParentDesc = item.ParentDesc,
                Area = tenClassItem.Area,
                AreaPrice = tenClassItem.AreaPrice,
                BuyPrice = tenClassItem.BuyPrice,
                CheapPrice = tenClassItem.CheapPrice,
                Inventory = tenClassItem.Inventory,
                MemberPrice = tenClassItem.MemberPrice,
                Pcount = item.Pcount,
                RealMaterialPrice = tenClassItem.RealMaterialPrice,
                Requirement = item.Requirement,
                Width = item.Width,
                SalePrice = tenClassItem.SalePrice,
                ParentId = item.ParentId,
                EightClassId = item.Id,
                NineClassId = nineClassItem.Id,
                TenClassId = tenClassItem.Id,
            };
        }

        private void dgClass1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            var datagrid = sender as DataGridViewX;
            if (datagrid == null) return;
            var gridGroup = datagrid.Tag.ToString();
            if (e is DataGridViewCellEventArgs)
            {
                var cellEvent = e as DataGridViewCellEventArgs;
                if (cellEvent.ColumnIndex >= 0 && cellEvent.RowIndex >= 0)
                {
                    if (SingleSelect)
                    {
                        for (int i = 0; i < datagrid.RowCount; ++i)
                        {
                            var firstCell = datagrid.Rows[i].Cells[0] as DataGridViewCheckBoxCell;
                            if (firstCell != null && i != cellEvent.RowIndex) firstCell.Value = firstCell.FalseValue;
                        }
                    }
                    int index = int.Parse(datagrid.Tag.ToString());
                        var cell = datagrid.Rows[e.RowIndex].Cells[0] as DataGridViewCheckBoxCell;
                        if (cell != null)
                        {
                            cell.Value = (cell.Value == cell.TrueValue ? cell.FalseValue : cell.TrueValue);
                            var bindObject = datagrid.Rows[e.RowIndex].DataBoundItem;
                            var id = bindObject?.GetType().GetProperty("Id").GetValue(bindObject) as int?;
                            if(cell.Value == cell.TrueValue)
                            {
                                if (SelectedIds[index] == null || SingleSelect)  SelectedIds[index] = new HashSet<int> { id.Value };
                                else SelectedIds[index].Add(id.Value);
                                OnItemSelectionChanged?.Invoke(ItemOperationType.OpSelected, index, bindObject as Data.Common.Entity.BaseEntity);
                            }
                            else
                            {
                                if (SelectedIds != null) SelectedIds[index].Remove(id.Value);
                            }
                        }
                        switch(index)
                    {
                        case 1:
                            if (SelectedIds[1] != null && SelectedIds[1].Count > 0) this.dgClass2.DataSource = Service.GetSysProductionSecondClassByParentIds(SelectedIds[1]);
                            else this.dgClass2.DataSource = null;
                            break;
                        case 2:
                            if (SelectedIds[2] != null && SelectedIds[2].Count > 0) this.dgClass3.DataSource = Service.GetSysProductionThirdClassByParentIds(SelectedIds[2]);
                            else this.dgClass3.DataSource = null;
                            break;
                        case 3:
                            if (SelectedIds[3] != null && SelectedIds[3].Count > 0) this.dgClass4.DataSource = Service.GetSysProductionFourClassByParentIds(SelectedIds[3]);
                            else this.dgClass4.DataSource = null;
                            break;
                        case 4:
                            if (SelectedIds[4] != null && SelectedIds[4].Count > 0) this.dgClass5.DataSource = Service.GetSysProductionFiveClassByParentIds(SelectedIds[4]);
                            else this.dgClass5.DataSource = null;
                            break;
                        case 5:
                            if (SelectedIds[5] != null && SelectedIds[5].Count > 0) this.dgClass6.DataSource = Service.GetSysProductionSixClassByParentIds(SelectedIds[5]);
                            else this.dgClass6.DataSource = null;
                            break;
                        case 6:
                            if (SelectedIds[6] != null && SelectedIds[6].Count > 0) this.dgClass7.DataSource = Service.GetSysProductionSevenClassByParentIds(SelectedIds[6]);
                            else this.dgClass7.DataSource = null;
                            break;
                        case 7:
                            if (SelectedIds[7] != null && SelectedIds[7].Count > 0) this.dgClass8.DataSource = Service.GetSysProductionEightClassByParentIds(SelectedIds[7]);
                            else this.dgClass8.DataSource = null;
                            break;
                        case 8:
                            if (SelectedIds[8] != null && SelectedIds[8].Count > 0) this.dgClass9.DataSource = Service.GetSysProductionNineClassByParentIds(SelectedIds[8]);
                            else this.dgClass9.DataSource = null;
                            break;
                        case 9:
                            if (SelectedIds[9] != null && SelectedIds[9].Count > 0) this.dgClass10.DataSource = Service.GetSysProductionTenClassByParentIds(SelectedIds[9]);
                            else this.dgClass10.DataSource = null;
                            break;
                    }
                }
                else
                {
                    return;
                }
            }
        }

        private void dgClass1_DataSourceChanged(object sender, EventArgs e)
        {
            var datagrid = sender as DataGridViewX;
            if (datagrid == null) return;
            var gridGroup = datagrid.Tag.ToString();
            if (string.IsNullOrEmpty(gridGroup)) return;
            var oldIds = SelectedIds[int.Parse(gridGroup)];
            var newIds = new HashSet<int>();
            switch(gridGroup)
            {
                case "1":
                    for(int i = 0; i < dgClass1.RowCount; ++i)
                    {
                        var id = datagrid.Rows[i].DataBoundItem?.GetType().GetProperty("Id").GetValue(datagrid.Rows[i].DataBoundItem) as int?;
                        if (oldIds != null && oldIds.Contains(id.Value)) newIds.Add(id.Value);
                    }
                    SelectedIds[1] = newIds;
                    if (newIds.Count <= 0) dgClass2.DataSource = null;
                    else if (!Array.Equals(oldIds, newIds)) dgClass2.DataSource = Service.GetSysItemSecondClassByParentIds(newIds);
                    break;
                case "2":
                    for(int i = 0; i < dgClass2.RowCount; ++i)
                    {
                        var id = datagrid.Rows[i].DataBoundItem?.GetType().GetProperty("Id").GetValue(datagrid.Rows[i].DataBoundItem) as int?;
                        if (oldIds != null && oldIds.Contains(id.Value)) newIds.Add(id.Value);
                    }
                    SelectedIds[2] = newIds;
                    if (newIds.Count <= 0) dgClass3.DataSource = null;
                    else if (!Array.Equals(oldIds, newIds)) dgClass3.DataSource = Service.GetSysItemThirdClassByParentIds(newIds);
                    break;
                case "3":
                    for(int i = 0; i < dgClass3.RowCount; ++i)
                    {
                        var id = datagrid.Rows[i].DataBoundItem?.GetType().GetProperty("Id").GetValue(datagrid.Rows[i].DataBoundItem) as int?;
                        if (oldIds != null && oldIds.Contains(id.Value)) newIds.Add(id.Value);
                    }
                    SelectedIds[3] = newIds;
                    if (newIds.Count <= 0) dgClass4.DataSource = null;
                    else if (!Array.Equals(oldIds, newIds)) dgClass4.DataSource = Service.GetSysItemFourClassByParentIds(newIds);
                    break;
                case "4":
                    for(int i = 0; i < dgClass4.RowCount; ++i)
                    {
                        var id = datagrid.Rows[i].DataBoundItem?.GetType().GetProperty("Id").GetValue(datagrid.Rows[i].DataBoundItem) as int?;
                        if (oldIds != null && oldIds.Contains(id.Value)) newIds.Add(id.Value);
                    }
                    SelectedIds[4] = newIds;
                    if (newIds.Count <= 0) dgClass5.DataSource = null;
                    else if (!Array.Equals(oldIds, newIds)) dgClass5.DataSource = Service.GetSysItemFiveClassByParentIds(newIds);
                    break;
                case "5":
                    for(int i = 0; i < dgClass5.RowCount; ++i)
                    {
                        var id = datagrid.Rows[i].DataBoundItem?.GetType().GetProperty("Id").GetValue(datagrid.Rows[i].DataBoundItem) as int?;
                        if (oldIds != null && oldIds.Contains(id.Value)) newIds.Add(id.Value);
                    }
                    SelectedIds[5] = newIds;
                    if (newIds.Count <= 0) dgClass6.DataSource = null;
                    else if (!Array.Equals(oldIds, newIds)) dgClass6.DataSource = Service.GetSysItemSixClassByParentIds(newIds);
                    break;
                case "6":
                    for(int i = 0; i < dgClass6.RowCount; ++i)
                    {
                        var id = datagrid.Rows[i].DataBoundItem?.GetType().GetProperty("Id").GetValue(datagrid.Rows[i].DataBoundItem) as int?;
                        if (oldIds != null && oldIds.Contains(id.Value)) newIds.Add(id.Value);
                    }
                    SelectedIds[6] = newIds;
                    if (newIds.Count <= 0) dgClass7.DataSource = null;
                    else if (!Array.Equals(oldIds, newIds)) dgClass7.DataSource = Service.GetSysItemSevenClassByParentIds(newIds);
                    break;
                case "7":
                    for(int i = 0; i < dgClass7.RowCount; ++i)
                    {
                        var id = datagrid.Rows[i].DataBoundItem?.GetType().GetProperty("Id").GetValue(datagrid.Rows[i].DataBoundItem) as int?;
                        if (oldIds != null && oldIds.Contains(id.Value)) newIds.Add(id.Value);
                    }
                    SelectedIds[7] = newIds;
                    if (newIds.Count <= 0) dgClass8.DataSource = null;
                    else if (!Array.Equals(oldIds, newIds)) dgClass8.DataSource = Service.GetSysItemEightClassByParentIds(newIds);
                    break;
                case "8":
                    for(int i = 0; i < dgClass8.RowCount; ++i)
                    {
                        var id = datagrid.Rows[i].DataBoundItem?.GetType().GetProperty("Id").GetValue(datagrid.Rows[i].DataBoundItem) as int?;
                        if (oldIds != null && oldIds.Contains(id.Value)) newIds.Add(id.Value);
                    }
                    SelectedIds[8] = newIds;
                    if (newIds.Count <= 0) dgClass9.DataSource = null;
                    else if (!Array.Equals(oldIds, newIds)) dgClass9.DataSource = Service.GetSysItemNineClassByParentIds(newIds);
                    break;
                case "9":
                    for(int i = 0; i < dgClass9.RowCount; ++i)
                    {
                        var id = datagrid.Rows[i].DataBoundItem?.GetType().GetProperty("Id").GetValue(datagrid.Rows[i].DataBoundItem) as int?;
                        if (oldIds != null && oldIds.Contains(id.Value)) newIds.Add(id.Value);
                    }
                    SelectedIds[9] = newIds;
                    if (newIds.Count <= 0) dgClass10.DataSource = null;
                    else if (!Array.Equals(oldIds, newIds)) dgClass10.DataSource = Service.GetSysItemNineClassByParentIds(newIds);
                    break;
            }
        }
    }
}
